/**
 * @Company:中享思途   
 * @Title:FrontUserController.java 
 * @Author:IL Clown   
 * @Date:2020年8月19日 上午9:10:29     
 */
package com.situ.shopsystem.controller;

import java.io.Serializable;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.situ.shopsystem.pojo.Catalog;
import com.situ.shopsystem.pojo.Product;
import com.situ.shopsystem.pojo.User;
import com.situ.shopsystem.service.CatalogService;
import com.situ.shopsystem.service.ProductService;
import com.situ.shopsystem.service.UserService;

/** 
 * @ClassName:FrontUserController 
 * @Description:前端用作登录
 * 
 * 
 * 
 * shop/doRegister
 * shop/doLogin
 * shop/doCancel
 * shop/goDetails
 * shop/goLiebiao
 * shop/goIndex
 * 
 */
@Controller
@RequestMapping("/shop/")
public class FrontUserController implements Serializable {
	private static final long serialVersionUID = 1L;
	@Autowired
	private UserService userService;
	@Autowired
	private CatalogService catalogService;
	@Autowired
	private ProductService productService;

	@RequestMapping("goIndex")
	public String goIndex(Model model) {
		List<Catalog> list = catalogService.findAllByParent(-1L);
		List<Catalog> catalog = catalogService.findAll();
		model.addAttribute("fristList", list);
		model.addAttribute("secondList", catalog);
		return "shop/index";
	}

	@RequestMapping("goDetails")
	public String goDetails(Long rowId, Model model) {
		Product product = productService.findOne(rowId);
		model.addAttribute("productDetail", product);
		return "shop/details";
	}

	@RequestMapping("goLiebiao")
	public String goLiebiao(Model model) {
		List<Product> list = productService.findAllBy();
		model.addAttribute("productList", list);
		return "shop/liebiao";
	}

	@RequestMapping("goLogin")
	public String goLogin() {
		return "shop/login";
	}

	@RequestMapping("goRegister")
	public String goRegister() {
		return "shop/register";
	}

	@RequestMapping("doLogin")
	public String doLogin(String userId, String userPass, HttpSession session) {
		User user = userService.findByIdAndPass(userId, userPass);
		if (user != null) {
			session.setAttribute("user", user);
			return "redirect:goIndex";

		} else {
			return "redirect:goLogin";
		}
	}

	@RequestMapping("doRegister")
	public String doRegister(User user, HttpSession session) {
		userService.saveUser(user, session);
		return "redirect:goLogin";
	}

	@RequestMapping("doCancel")
	public String doCancel(User user, HttpSession session) {
		session.removeAttribute("user");
		return "redirect:goIndex";
	}

}
